import api from "@/api/apiServer";

let sourceData;
async function getData() {
  let res = await api.DictFindByCode({ parentCode: "dict.source" });
  sourceData = res.data;
}
getData();

// 格式化数据来源
function caseSource(value, option) {
  let result = option.filter((item) => {
    return item.id === value;
  });

  if (result.length > 0) {
    return result[0].dictName;
  } else {
    return "";
  }
}

const columns = [
  {
    title: "级别",
    dataIndex: "name",
    key: "name",
    customRender: (text, record, index) => `${record.level + "级"}`,
    align: "center",
  },
  {
    title: "商品编号",
    dataIndex: "goodsCode",
    key: "goodsCode",
    align: "center",
  },
  {
    title: "商品名称",
    dataIndex: "bomName",
    key: "bomName",
    align: "center",
  },
  {
    title: "商品规格",
    key: "specifications",
    dataIndex: "specifications",
    align: "center",
  },
  {
    title: "商品类型",
    key: "goodsTypeName",
    dataIndex: "goodsTypeName",
    align: "center",
  },
  {
    title: "来源",
    key: "source",
    dataIndex: "source",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text,sourceData);
    },
  },
  {
    title: "数量",
    key: "quantity",
    dataIndex: "quantity",
    align: "center",
  },
  {
    title: "工序数",
    key: "bomWorkStageNum",
    dataIndex: "bomWorkStageNum",
    align: "center",
  },
  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    fixed: "right",
    width: 200,
    scopedSlots: { customRender: "action" },
  },
];

const workColumns = [
  {
    title: "工序序号",
    dataIndex: "name",
    key: "name",
    align: "center",
  },
  {
    title: "工序名称",
    dataIndex: "path",
    key: "path",
    align: "center",
  },
  {
    title: "工序单价",
    dataIndex: "paths",
    key: "paths",
    align: "center",
  },
  {
    title: "工时",
    key: "type2",
    dataIndex: "type2",
    align: "center",
  },
  {
    title: "备注",
    key: "type2s",
    dataIndex: "types",
    align: "center",
  },
  {
    title: "操作",
    dataIndex: "action",
    align: "center",
    scopedSlots: { customRender: "action" },
  },
];

const goodColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "商品编号",
    dataIndex: "goodsCode",
    key: "goodsCode",
    align: "center",
  },
  {
    title: "商品名称",
    dataIndex: "goodsName",
    key: "goodsName",
    align: "center",
  },
  {
    title: "商品类型",
    key: "goodsTypeName",
    dataIndex: "goodsTypeName",
    align: "center",
  },
  {
    title: "计量单位",
    key: "unitName",
    dataIndex: "unitName",
    align: "center",
  },
  {
    title: "来源",
    key: "source",
    dataIndex: "source",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text,sourceData);
    },
  },
  {
    title: "规格",
    key: "specifications",
    dataIndex: "specifications",
    align: "center",
  },
  {
    title: "品牌",
    key: "brand",
    dataIndex: "brand",
    align: "center",
  },
  {
    title: "是否生成BOM",
    key: "isUsedBom",
    dataIndex: "isUsedBom",
    customRender: (text, record, index) => {
      return caseBom(text);
    },
    align: "center",
  },
];

function caseBom(type) {
  switch (type) {
    case 0:
      return "否";
    case 1:
      return "是";
  }
}
export { columns, workColumns, goodColumns };
